/**
 * 拼接对象为请求字符串
 * @param {Object} obj - 待拼接的对象
 * @returns {string} - 拼接成的请求字符串
 * Auth:WANGJIAN
 */
export function encodeSearchParams(obj = {}) {
  const params: string[] = []

  Object.keys(obj).forEach((key) => {
    let value = obj[key]
    // 如果值为undefined我们将其置空
    if (typeof value === 'undefined') {
      value = ''
    }
    // 对于需要编码的文本（比如说中文）我们要进行编码
    params.push([key, encodeURIComponent(value)].join('='))
  })

  return params.join('&')
}

export function isNil(value: any) {
  return value === null || value === undefined
}

export function isNilSync(value: any) {
  return new Promise((resolve, reject) => {
    while (1) {
      const isNil = value === null || value === undefined
      if (!isNil) {
        resolve(value)
        break
      }
    }
  })
}